package repository

import (
	"gorm.io/gorm"
	"guliedu/model"
)

type IStatisticsDailyRepository interface {
	// 增加统计
	Insert(daily *model.StatisticsDaily) error
	// 更新统计
	Update(daily *model.StatisticsDaily) error
	// 查询统计
	Select(date string) (*model.StatisticsDaily, error)
	// 查询时间内的统计
	SelectByType(date []string) ([]model.StatisticsDaily, error)
}

func NewIStatisticsDailyRepository() IStatisticsDailyRepository {
	return &StatisticsDailyRepository{mysqlDb: db}
}

type StatisticsDailyRepository struct {
	mysqlDb *gorm.DB
}

func (t *StatisticsDailyRepository) Insert(daily *model.StatisticsDaily) error {
	return t.mysqlDb.Create(&daily).Error
}

func (t *StatisticsDailyRepository) Update(daily *model.StatisticsDaily) error {
	return t.mysqlDb.Save(&daily).Error
}

func (t *StatisticsDailyRepository) Select(date string) (daily *model.StatisticsDaily, err error) {
	err = t.mysqlDb.Where("date_calculated = ?", date).Find(&daily).Error
	return
}

func (t *StatisticsDailyRepository) SelectByType(date []string) (dailys []model.StatisticsDaily, err error) {
	err = t.mysqlDb.Where("date_calculated IN ?", date).Find(&dailys).Error
	return
}
